var refreshGirdData; // 更新数据
var companyId = '';
var ztId = '';
var userdefault = {};
var params = {};
var bootstrap = function ($, learun) {

    var pageWidth = 1024;

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            pageWidth = ($('.lr-layout-grid').width());

            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;

                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'gzzts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;

                                    learun.httpPost2(config.base_server() + 'gzenablemonths/findByZtId?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token, {}, function (res) {
                                        learun.loading(false);
                                        if (res.resp_code == 200) {
                                            $("#lb_current").html("当前月份:" + res.data.currentMonth)
                                        }
                                    });

                                    page.initGrid();
                                    page.search();
                                }
                            }
                        }).lrselectSet(userdefault.ztId);

                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //执行计算
            $("#lr_calc").on('click', function () {
                learun.layerConfirm('是否确认重新计算工资?', function (res) {
                    if (res) {
                        learun.postForm(config.base_server() + 'gzrecordsalarys/calc?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token, {
                            companyId: companyId,
                            ztId: ztId
                        }, function () {
                            refreshGirdData();
                        });
                    }
                });
            });
            //Excel导入
            $("#lr_import").on('click', function () {
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传文件',
                    url: config.webportal_server + 'pages/salary/recordTax/uploadifyForm.html?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (datas) {
                            if (datas) {
                                refreshGirdData();
                            }
                        });
                    }
                });
            });
            //Excel导出
            $("#lr_export").on('click', function () {
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'gzrecordtaxs/exportExcel',
                    param: params,
                    method: 'GET'
                });
            });
            //保存
            $("#lr_save").on('click', function () {
                params.access_token = config.getToken().access_token;
                var productData = [];
                var productDataTmp = $('#gridtable').jfGridGet('rowdatas');
                for (var i = 0, l = productDataTmp.length; i < l; i++) {
                    productData.push(productDataTmp[i]);
                }

                var postData = productData;
                $.lrSaveForm(config.base_server() + 'gzrecordtaxs/saveOrUpdate2?companyId=' + companyId+ '&ztId=' + ztId + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
                    if (res.resp_code == 200) {
                        refreshGirdData();
                    }
                });
            });
        },
        initGrid: function (headData) {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'gzrecordtaxs/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "部门", name: "departmentname", width: 120, align: "left"},
                    {label: "员工编号", name: "usercode", width: 80, align: "left"},
                    {label: "姓名", name: "username", width: 120, align: "left"},
                    {
                        label: "工资", name: "salary", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var salary = data.salary == null ? 0 : Number(data.salary);
                                var bonus1 = data.bonus1 == null ? 0 : Number(data.bonus1);
                                var bonus2 = data.bonus2 == null ? 0 : Number(data.bonus2);
                                var bonus3 = data.bonus3 == null ? 0 : Number(data.bonus3);
                                var bonus4 = data.bonus4 == null ? 0 : Number(data.bonus4);
                                var bonus5 = data.bonus5 == null ? 0 : Number(data.bonus5);
                                data.income = salary + bonus1 + bonus2 + bonus3 + bonus4 + bonus5;

                                $('#gridtable').jfGridSet('updateRow', num);

                                page.calcTax(data.income, data, num);

                            }
                        }
                    },
                    {
                        label: "奖金1", name: "bonus1", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var salary = data.salary == null ? 0 : Number(data.salary);
                                var bonus1 = data.bonus1 == null ? 0 : Number(data.bonus1);
                                var bonus2 = data.bonus2 == null ? 0 : Number(data.bonus2);
                                var bonus3 = data.bonus3 == null ? 0 : Number(data.bonus3);
                                var bonus4 = data.bonus4 == null ? 0 : Number(data.bonus4);
                                var bonus5 = data.bonus5 == null ? 0 : Number(data.bonus5);
                                data.income = salary + bonus1 + bonus2 + bonus3 + bonus4 + bonus5;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "奖金2", name: "bonus2", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var salary = data.salary == null ? 0 : Number(data.salary);
                                var bonus1 = data.bonus1 == null ? 0 : Number(data.bonus1);
                                var bonus2 = data.bonus2 == null ? 0 : Number(data.bonus2);
                                var bonus3 = data.bonus3 == null ? 0 : Number(data.bonus3);
                                var bonus4 = data.bonus4 == null ? 0 : Number(data.bonus4);
                                var bonus5 = data.bonus5 == null ? 0 : Number(data.bonus5);
                                data.income = salary + bonus1 + bonus2 + bonus3 + bonus4 + bonus5;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "奖金3", name: "bonus3", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var salary = data.salary == null ? 0 : Number(data.salary);
                                var bonus1 = data.bonus1 == null ? 0 : Number(data.bonus1);
                                var bonus2 = data.bonus2 == null ? 0 : Number(data.bonus2);
                                var bonus3 = data.bonus3 == null ? 0 : Number(data.bonus3);
                                var bonus4 = data.bonus4 == null ? 0 : Number(data.bonus4);
                                var bonus5 = data.bonus5 == null ? 0 : Number(data.bonus5);
                                data.income = salary + bonus1 + bonus2 + bonus3 + bonus4 + bonus5;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "奖金4", name: "bonus4", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var salary = data.salary == null ? 0 : Number(data.salary);
                                var bonus1 = data.bonus1 == null ? 0 : Number(data.bonus1);
                                var bonus2 = data.bonus2 == null ? 0 : Number(data.bonus2);
                                var bonus3 = data.bonus3 == null ? 0 : Number(data.bonus3);
                                var bonus4 = data.bonus4 == null ? 0 : Number(data.bonus4);
                                var bonus5 = data.bonus5 == null ? 0 : Number(data.bonus5);
                                data.income = salary + bonus1 + bonus2 + bonus3 + bonus4 + bonus5;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "奖金5", name: "bonus5", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var salary = data.salary == null ? 0 : Number(data.salary);
                                var bonus1 = data.bonus1 == null ? 0 : Number(data.bonus1);
                                var bonus2 = data.bonus2 == null ? 0 : Number(data.bonus2);
                                var bonus3 = data.bonus3 == null ? 0 : Number(data.bonus3);
                                var bonus4 = data.bonus4 == null ? 0 : Number(data.bonus4);
                                var bonus5 = data.bonus5 == null ? 0 : Number(data.bonus5);
                                data.income = salary + bonus1 + bonus2 + bonus3 + bonus4 + bonus5;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "收入合计", name: "income", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "扣社保费", name: "socialSecurityFund", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "扣公积金", name: "accumulationFund1", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.accumulationMoney = accumulationFund1 + accumulationFund2;
                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "扣公积金2", name: "accumulationFund2", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.accumulationMoney = accumulationFund1 + accumulationFund2;
                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "公积金实际扣除", name: "accumulationMoney", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "扣医疗保险", name: "healthInsurance", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "扣养老保险", name: "endowmentInsurance", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "扣其他", name: "other", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "允许扣除费用", name: "allowFee", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "扣捐赠额", name: "donate", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number",
                            change: function (data, num) {// 行数据和行号
                                var socialSecurityFund = data.socialSecurityFund == null ? 0 : Number(data.socialSecurityFund);
                                var accumulationFund1 = data.accumulationFund1 == null ? 0 : Number(data.accumulationFund1);
                                var accumulationFund2 = data.accumulationFund2 == null ? 0 : Number(data.accumulationFund2);
                                var healthInsurance = data.healthInsurance == null ? 0 : Number(data.healthInsurance);
                                var endowmentInsurance = data.endowmentInsurance == null ? 0 : Number(data.endowmentInsurance);
                                var other = data.other == null ? 0 : Number(data.other);
                                var allowFee = data.allowFee == null ? 0 : Number(data.allowFee);
                                var donate = data.donate == null ? 0 : Number(data.donate);

                                data.cutTotal = socialSecurityFund + accumulationFund1 + accumulationFund2 + healthInsurance + endowmentInsurance + other + allowFee + donate;
                                $('#gridtable').jfGridSet('updateRow', num);
                            }
                        }
                    },
                    {
                        label: "扣款合计", name: "cutTotal", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "应纳所得税额", name: "taxableIncome", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "税率", name: "tax", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue + "%";
                        }
                    },
                    {
                        label: "扣除数", name: "deduction", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "应纳税额", name: "taxAmount", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "减免税额", name: "taxDeduction", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number"
                        }
                    },
                    {
                        label: "财政预扣税额", name: "fiscalCut", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number"
                        }
                    },
                    {
                        label: "单位预扣税额", name: "companyCut", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number"
                        }
                    },
                    {
                        label: "补税额", name: "fillTax", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        },
                        edit: {
                            type: "number"
                        }
                    },
                    {
                        label: "实际纳税额", name: "actualTax", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    }
                ],
                isPage: true,
                reloadSelected: true,
                isEdit: true,
                mainId: 'id',
                parentId: 'parentId',
            });
        },

        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.ztId = ztId;
            params = param;

            $('#gridtable').jfGridSet('reload', param);
        },
        //计算税率
        calcTax: function (income, data, num) {
            if (!income) {
                return;
            }
            $.ajax({
                url: config.base_server() + 'gztaxs/calcTax?companyId=' + companyId + '&income=' + income + '&isIncludeTax=' + 1 + '&access_token=' + config.getToken().access_token,
                data: {},
                type: 'GET',
                //  dataType: "json",
                success: function (res) {
                    data.taxableIncome = res.data.taxableIncome;
                    data.tax = res.data.tax;
                    data.taxAmount = res.data.taxAmount;
                    data.deduction = res.data.deduction;
                    data.actualTax = res.data.actualTax;
                    $('#gridtable').jfGridSet('updateRow', num);
                }
            });
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


